/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Gtk;

namespace Whitemice.ZOGI.GtkFolios
{  
	[System.ComponentModel.Category("widget")]
	[System.ComponentModel.ToolboxItem(true)]
	public class NoteTab : Gtk.ScrolledWindow, IStorableEntityForm
	{ 
	  protected Dictionary<int, NoteForm>           forms;
	  protected VBox 						                    vbox = null;
	  protected string                              context;
	  
	  public    EntityEventHandler                  OnEntityPresent;
	  public    EntityEventHandler                  OnNoteDelete;
	  
		public NoteTab(string context)
		{
		  Viewport  viewport;
		  
		  this.context = context;
			this.HscrollbarPolicy = PolicyType.Automatic;
			this.VscrollbarPolicy = PolicyType.Always;
			forms = new Dictionary<int, NoteForm>();
			vbox = new VBox();
			vbox.Spacing = 2;
			viewport = new Viewport();
			viewport.Child = vbox;
			this.Child = viewport;
		} // end ctor
		
		public void Clear()
		{
      var children = vbox.Children;
      foreach(Widget widget in children)
        vbox.Remove(widget);
      forms.Clear();
		}	// end Clear 
		
		public void Fill(Entity entity)
		{
			int               c;
			NoteForm          form;
			IEnumerable<Note> notes;
								
			if (!(entity is INoteContainer))
				throw new Exception("Inappropriate entity provided to form");					
			
			Clear();
			c = 0;
			notes = (entity as INoteContainer).Notes;
			if (notes.Count() > 0)
			{
			  foreach(Whitemice.ZOGI.Note note in notes)
			  {
          this.ZLOG(string.Format("Adding note#{0} to tab for {1}#{2}",
			      note.ObjectId,
			      entity.EntityName,
			      entity.ObjectId));
				  form = new Whitemice.ZOGI.GtkWidgets.NoteForm(note, context);
				  form.OnEntityPresent = this.OnEntityPresent;
				  form.OnNoteDelete = this.OnNoteDelete;
				  form.AutoSensitive();
   	      if ((c % 2) == 1)
 	          form.BackgroundColor = GUI.ColorForKey("oddRowBackground");
 	        else
 	          form.BackgroundColor = GUI.ColorForKey("evenRowBackground");
 	    	  vbox.PackStart(form, false, false, 2);
 	    	  c++;
 	    	  form.ScrollPolicy(PolicyType.Never, PolicyType.Never);
 	    	  forms.Add(note.ObjectId, form);
 	      } // end foreach-note-in-notes
 	    } else
 	      {
 	        vbox.PackStart(new Label("No notes for object"), false, false, 2);
 	      }
			ShowAll();
		} // end Fill
		
		public bool Take(Entity entity)
		{
		  INoteContainer  container;
		  NoteForm        form;
		  
      if (entity is INoteContainer)
      {
        container = (entity as INoteContainer);
        foreach(int objectId in forms.Keys)
        {
          form = forms[objectId];
          foreach(Note note in container.Notes)
          {
            if (note.ObjectId == form.ObjectId)
            {
              form.Take(note);
              break;
            } // end if-note-matches
          } // end foreach-note-in-container
        } // end foreach-form-in-forms
      } else throw new Exception("Inappropriate entity provided to form");
      return true;
		} // end Take
		
		public void InsertNote(Note note)
		{
		  NoteForm  form;
		  
		  foreach(int objectId in forms.Keys)
		    if (note.ObjectId == objectId)
		      return;
		  form = new NoteForm(note, context);
		  forms.Add(note.ObjectId, form);
		  vbox.PackStart(form, false, false, 2);
		} // end InsertNote
		
	} // end NoteTab
} // end namespace